#include "ai.h"
#include "hud.h"

AI::AI()
{
	round = 0;
	count = 0;
	timer = sf::Clock();
}

void AI::registerUserControled(Player* player)
{
	players[3].player = player;
}

void AI::registerAIControled(Player* player, unsigned char level)
{
	players[count].player = player;
	players[count].level = level;
	count++;
}

void AI::setup()
{
	for (int i = 0; i < count; i++)
	{
		players[i].jumpTime = sf::Randomizer::Random(0.15f, 0.38f);
		players[i].slowHeight = sf::Randomizer::Random(200, 280);		
	}
	round++;
	isGreen = false;
}

void AI::update(unsigned char state)
{
	if (!isGreen)
	{
		if (state == HS_GREEN)
		{
			isGreen = true;
			timer.Reset();
		}
	}
	else
	{
		for (int i = 0; i < count; i++)
		{
			if (players[i].player->getState() == PS_START && players[i].jumpTime < timer.GetElapsedTime()) 
				players[i].player->nextState();
			if (players[i].player->getHeight() < players[i].slowHeight && players[i].player->getState() == PS_FREE_FALL)
				players[i].player->nextState();
		}
	}
}

AI::~AI()
{
}
